﻿
using Iworld.Backstage.Framework.AdministratorService;
using Iworld.ViewModel;
using System.Runtime.Serialization;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 登陆页的视图模型
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        #region 私有字段

        string _account = "";
        string _password = "";
        string _loadingMessage = "";
        bool rememberMe;
        #endregion

        #region 公开属性

        public bool RememberMe
        {
            get
            {
                return rememberMe;
            }
            set
            {
                if (rememberMe != value)
                {
                    rememberMe = value;
                    OnPropertyChanged("RememberMe");
                }
            }
        }
        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get
            {
                return _account;
            }
            set
            {
                if (_account != value)
                {
                    _account = value;
                    OnPropertyChanged("Account");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        /// <summary>
        /// 加载时的文字显示
        /// </summary>
        public string LoadingMessage
        {
            get
            {
                return _loadingMessage;
            }
            set
            {
                if (_loadingMessage != value)
                {
                    _loadingMessage = value;
                    OnPropertyChanged("LoadingMessage");
                }
            }
        }

        /// <summary>
        /// 登陆通知
        /// </summary>
        public UniversalCommand LoginCommand { get; set; }

        /// <summary>
        /// 跳转命令
        /// </summary>
        public UniversalCommand JumpCommand { get; set; }

        #endregion

        /// <summary>
        /// 实例化一个新的登陆页的视图模型
        /// </summary>
        public LoginViewModel()
        {
            this.LoginCommand = new UniversalCommand(Login);
            this.JumpCommand = new UniversalCommand(x=> BackstageViewModelService.Current.JumpTo(x.ToString()));
            bool hadRememberMe = DataManager.HaveValue<LoginPackage>(DataKey.RememberMe,true);
            if (hadRememberMe)
            {
                LoginPackage package = DataManager.GetValue<LoginPackage>(DataKey.RememberMe,true);
                this.Account = package.Account;
                this.Password = package.Password;
                this.RememberMe = true;
            }
        }

        void Login(object parameter)
        {
           if (IsBusy)
            {
                return;
            }
            this.LoadingMessage = "正在登录请稍后......";
            IsBusy = true;
            
            AdministratorServiceClient asc = new AdministratorServiceClient();
            asc.LoginCompleted +=  LoginCompleted;
            asc.LoginAsync(new LoginImport() {
                Account = this.Account,
                Password = this.Password,
            });

        }

        void LoginCompleted(object sender, LoginCompletedEventArgs e)
        {

            if (e.Result.Success)
            {
                if (this.RememberMe == true)
                {
                    LoginPackage package = new LoginPackage(this.Account, this.Password);
                    DataManager.SetValue(DataKey.RememberMe, package, true);
                }
                else
                {
                    DataManager.RemoveValue(DataKey.RememberMe, true);
                }
                AdministratorExport ae = e.Result.Info;
                DataManager.SetValue(DataKey.Administrator_Info, ae);
                BackstageViewModelService.Current.JumpTo(Page.首页);
            }
            else
            {
                string error = e.Result.Error;
                this.ShowError(error);
            }
            IsBusy = false;
        }

    }

    /// <summary>
    /// 用户登陆信息的封装
    /// </summary>
    [DataContract]
    public class LoginPackage
    {
        #region 属性

        /// <summary>
        /// 用户名
        /// </summary>
        [DataMember]
        public string Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [DataMember]
        public string Password { get; set; }

        #endregion

        #region 构造方法

        /// <summary>
        /// 实例化一个用户登陆信息的封装
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public LoginPackage(string acc, string password)
        {
            this.Account = acc;
            this.Password = password;
        }

        #endregion
    }
}
